package com.smforj.ssm.modules.sys.dao.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import com.smforj.ssm.frame.core.dao.impl.BaseDaoImpl;
import com.smforj.ssm.frame.core.exception.DaoException;
import com.smforj.ssm.frame.core.utils.BeanUtils;
import com.smforj.ssm.modules.lib.domain.IUser;
import com.smforj.ssm.modules.sys.dao.SysUserDao;
import com.smforj.ssm.modules.sys.domain.SysUser;
import com.smforj.ssm.modules.sys.domain.UserRole;
import com.smforj.ssm.modules.sys.domain.query.SysUserQuery;

/***
 * 用户信息实现对象 该类实现了注解,子系统可以再次实现该对象
 * 
 * @author Haijun Gao 
 * @date 2016-9-8 上午9:35:18
 * @desp http://www.cnblogs.com/saga5998/
 * @email 573107773@qq.com
 */
@Repository
public class SysUserDaoImpl extends BaseDaoImpl<SysUser> implements SysUserDao {

	/* (non-Javadoc)
	 * @see com.smforj.core.dao.SysUserDao#getUserAndRoles(SysUserQuery query)
	 */
	@Override 
	public IUser getUserAndRoles(SysUserQuery query) { 
		String statement = "selectUserAndRole";
		try {
			Map<String, Object> params = BeanUtils.toMap(query);
			return this.getSqlSessionTemplate().selectOne(getSqlName(statement), params);
		} catch (Exception e) {
			throw new DaoException(String.format("查询对象列表出错！语句：%s", getSqlName(statement)), e);
		}
	}
	/* (non-Javadoc)
	 * @see com.smforj.core.dao.SysUserDao#insertUserRoleInBatch(List<UserRole> entityList)
	 */
	@Override
	public int insertUserRoleInBatch(List<UserRole> entityList) {
		if (entityList == null || entityList.isEmpty())
			return -1;
		for (UserRole entity : entityList) {
			this.insertUserRole(entity);
		}
		return 0;
	}
	/* (non-Javadoc)
	 * @see com.smforj.core.dao.SysUserDao#removeUserRole(java.io.String)
	 */
	@Override
	public int removeUserRole(String userid) {
		Assert.notNull(userid);
		String statement = "removeUserRoleByUserId";
		try {
			return getSqlSessionTemplate().delete(getSqlName(statement), userid);
		} catch (Exception e) {
			throw new DaoException(String.format("根据ID删除对象出错！语句：%s", getSqlName(statement)), e);
		}
	}

	/* (non-Javadoc)
	 * @see com.smforj.core.dao.SysUserDao#insertUserRole(java.io.Serializable)
	 */
	@Override
	public int insertUserRole(UserRole entity) {
		String statement = "insertUserRole";
		Assert.notNull(entity);
		try {
			if (StringUtils.isBlank(entity.getId()))
				entity.setId(generateId());
			return getSqlSessionTemplate().insert(getSqlName(statement), entity);
		} catch (Exception e) {
			throw new DaoException(String.format("添加对象出错！语句：%s", getSqlName(statement)), e);
		} 
	} 
}
